project(CGame++_lib VERSION 1.0.0)


find_package(PkgConfig REQUIRED)


set(CGAME_SRC
  ${CGAME_SRC}

  ${CMAKE_CURRENT_LIST_DIR}/game_time.cpp
  ${CMAKE_CURRENT_LIST_DIR}/game_http.cpp
  ${CMAKE_CURRENT_LIST_DIR}/game_httpd.cpp
  ${CMAKE_CURRENT_LIST_DIR}/game_httpd_request.cpp
  ${CMAKE_CURRENT_LIST_DIR}/game_httpd_response.cpp
  ${CMAKE_CURRENT_LIST_DIR}/game_byte.cpp
  ${CMAKE_CURRENT_LIST_DIR}/game_byte_stack.cpp
  ${CMAKE_CURRENT_LIST_DIR}/game_byte_ring.cpp
  ${CMAKE_CURRENT_LIST_DIR}/game_version.cpp
  ${CMAKE_CURRENT_LIST_DIR}/game_application.cpp
  ${CMAKE_CURRENT_LIST_DIR}/game_string.cpp
  ${CMAKE_CURRENT_LIST_DIR}/game_file.cpp
  ${CMAKE_CURRENT_LIST_DIR}/game_yml.cpp
  ${CMAKE_CURRENT_LIST_DIR}/game_util.cpp
  ${CMAKE_CURRENT_LIST_DIR}/game_log.cpp
  ${CMAKE_CURRENT_LIST_DIR}/game_log_factory.cpp
  ${CMAKE_CURRENT_LIST_DIR}/game_event.cpp
  ${CMAKE_CURRENT_LIST_DIR}/game_socket.cpp
  ${CMAKE_CURRENT_LIST_DIR}/game_socketd.cpp
  ${CMAKE_CURRENT_LIST_DIR}/game_ev.cpp
  ${CMAKE_CURRENT_LIST_DIR}/game_tar.cpp
  ${CMAKE_CURRENT_LIST_DIR}/game_timer.cpp
  ${CMAKE_CURRENT_LIST_DIR}/game_cron.cpp
  ${CMAKE_CURRENT_LIST_DIR}/game_curl.cpp
  ${CMAKE_CURRENT_LIST_DIR}/game_spu.cpp
  ${CMAKE_CURRENT_LIST_DIR}/game_autorelease.cpp
  ${CMAKE_CURRENT_LIST_DIR}/game_def.cpp
  ${CGAME_ROOT}/lib/ccronexpr/ccronexpr.c
)




if(NOT CGAME_TARGET_PLATFORM)
  if(${CMAKE_HOST_SYSTEM_NAME} MATCHES "Linux")
    set(CGAME_TARGET_PLATFORM 1)
    pkg_check_modules(UUID REQUIRED uuid)
    set(CGAME_LIBRARIES uuid)
  elseif(${CMAKE_HOST_SYSTEM_NAME} MATCHES "Darwin")
    set(CGAME_TARGET_PLATFORM 2)
  else()
    set(CGAME_TARGET_PLATFORM 0)
  endif()
  set(CGAME_TARGET_PLATFORM ${CGAME_TARGET_PLATFORM} PARENT_SCOPE)



endif()

if((${CGAME_TARGET_PLATFORM} MATCHES 1) OR (${CGAME_TARGET_PLATFORM} MATCHES 2))

set(CGAME_SRC
  ${CGAME_SRC}
  ${CMAKE_CURRENT_LIST_DIR}/game_pthread.cpp
)

pkg_check_modules(LOG4C log4c)
if (NOT LOG4C_FOUND)
  message(STATUS "log4c not found in pkg-config, try to find it in library path")
  find_library(LOG4C_LIB NAMES log4c)
  if(NOT LOG4C_LIB)
    message(FATAL_ERROR "log4c not found")
  else()
    message(STATUS "LOG4C_LIBRARIES: ${LOG4C_LIB}")
    set(LOG4C_LIBRARIES -llog4c)
  endif()
endif()

pkg_check_modules(EVENT REQUIRED libevent libevent_pthreads)
pkg_check_modules(CURL REQUIRED libcurl)
# pkg_check_modules(CGAME_LIBRARIES REQUIRED libevent log4c uuid libcurl)
set(CGAME_LIBRARIES ${CGAME_LIBRARIES} pthread ${LOG4C_LIBRARIES} ${EVENT_LIBRARIES} ${CURL_LIBRARIES})
endif()



if(${CGAME_TARGET_PLATFORM} MATCHES 2)
  include_directories(/opt/homebrew/include)
  link_directories(/opt/homebrew/lib)
elseif(${CGAME_TARGET_PLATFORM} MATCHES 1)
  pkg_check_modules(UNWIND REQUIRED libunwind-generic)
  set(CGAME_LIBRARIES
    ${CGAME_LIBRARIES} ${UNWIND_LIBRARIES}
  )
endif()

if(${CGAME_TARGET_LOGGER} MATCHES "stdout")
set(CGAME_SRC
  ${CGAME_SRC}
  ${CMAKE_CURRENT_LIST_DIR}/game_log_stdout.cpp
)
endif()

set(CGAME_LIBRARIES ${CGAME_LIBRARIES} ryml microtar)

message(STATUS "")
message(STATUS "        ---( CGame++ v${CGAME_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH} )---")
message(STATUS "")
message(STATUS "CMAKE_HOST_SYSTEM_NAME:    ${CMAKE_HOST_SYSTEM_NAME}")
message(STATUS "CMAKE_HOST_SYSTEM_VERSION: ${CMAKE_HOST_SYSTEM_VERSION}")
message(STATUS "CMAKE_BUILD_TYPE:          ${CMAKE_BUILD_TYPE}")
message(STATUS "CGAME_TARGET_PLATFORM:     ${CGAME_TARGET_PLATFORM}")
message(STATUS "CMAKE_C_FLAGS:             ${CMAKE_C_FLAGS}")
message(STATUS "CMAKE_CXX_FLAGS:           ${CMAKE_CXX_FLAGS}")
message(STATUS "CMAKE_BUILD_TYPE:          ${CMAKE_BUILD_TYPE}")
message(STATUS "CMAKE_C_COMPILER:          ${CMAKE_C_COMPILER} (id ${CMAKE_C_COMPILER_ID}, clang ${CLANG}, GNUC ${GNUC})")
message(STATUS "CMAKE_AR:                  ${CMAKE_AR}")
message(STATUS "CMAKE_RANLIB:              ${CMAKE_RANLIB}")
message(STATUS "PROJECT_BINARY_DIR:        ${PROJECT_BINARY_DIR}")
message(STATUS "PROJECT_SOURCE_DIR:        ${PROJECT_SOURCE_DIR}")
message(STATUS "CGAME_DEBUG:               ${CGAME_DEBUG}")
message(STATUS "CGAME_TARGET_LOGGER:       ${CGAME_TARGET_LOGGER}")
message(STATUS "CGAME_INCLUDES:            ${CGAME_INCLUDES}")
message(STATUS "CGAME_LIBRARIES:           ${CGAME_LIBRARIES}")
message(STATUS "")

add_library(CGame++ STATIC ${CGAME_SRC})




if(${CMAKE_BUILD_TYPE} MATCHES "Debug")
target_compile_definitions(CGame++ PRIVATE _DEBUG -DCRON_USE_LOCAL_TIME)
endif()
target_link_libraries(CGame++ PUBLIC ${CGAME_LIBRARIES})
# target_compile_options(CGame++ PRIVATE "-fPIC")
target_include_directories(CGame++ PUBLIC ${CGAME_ROOT}/include ${CGAME_INCLUDES})
